<template>
  <div class="confirmOrder">
    <el-breadcrumb separator="/" class="breadcrumb-bottom">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>教材管理</el-breadcrumb-item>
      <el-breadcrumb-item>确认订单</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="20" class="mb-12">
      <el-col :span="5">
        <el-select
          class="full-line"
          v-model="form.majorId"
          filterable
          remote
          clearable
          placeholder="请选择专业名称">
          <el-option
            v-for="item in majorChoose"
            :label="item.majorName"
            :value="item.majorID">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="5">
        <el-date-picker
          @change="getGrade"
          v-model="form.grade"
          type="year"
          placeholder="请选择年级"
          class="full-line">
        </el-date-picker>
      </el-col>
      <el-col :span="5">
        <el-date-picker
          @change="getAcademicYear"
          v-model="form.academicYear"
          type="year"
          placeholder="请选择学年"
          class="full-line">
        </el-date-picker>
      </el-col>
      <el-col :span="5">
        <el-select
          clearable
          class="full-line"
          v-model="form.term"
          placeholder="请选择学期">
          <el-option label="01" value="01"></el-option>
          <el-option label="02" value="02"></el-option>
        </el-select>
      </el-col>
      <el-col :span="4" class="overflow-clear">
        <el-button type="primary" @click="searchOrder" class="float-right">立即搜索</el-button>
      </el-col>
    </el-row>
    <hr>
    <div class="overflow-clear">
      <el-button type="primary" class="float-right mb-5" @click="confirmOrder">确认</el-button>
    </div>
    <el-table
      :data="historyOrderTable"
      border
      style="width: 100%">
      <el-table-column
        prop="publishingHouse"
        label="出版社"
        min-width="130">
      </el-table-column>
      <el-table-column
        prop="textbookName"
        label="教材名称"
        min-width="130">
      </el-table-column>
      <el-table-column
        prop="author"
        label="作者"
        min-width="100">
      </el-table-column>
      <el-table-column
        prop="price"
        label="单价"
        min-width="100">
      </el-table-column>
      <el-table-column
        prop="isbn"
        label="ISBN"
        min-width="140">
      </el-table-column>
      <el-table-column
        prop="courseNameCN"
        label="课程名称"
        min-width="150">
      </el-table-column>
      <el-table-column
        prop="majorName"
        label="使用专业"
        min-width="130">
      </el-table-column>
      <el-table-column
        prop="orderNum"
        label="预定数量"
        min-width="100">
      </el-table-column>
      <el-table-column
        prop="remark1"
        label="备注"
        min-width="120">
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="120">
        <template scope="scope">
          <el-button icon="edit" type="text" @click="updateOeder(scope.$index,historyOrderTable)"></el-button>
          <el-button icon="delete" type="text"
                     @click="deleteOrder(scope.$index,historyOrderTable,scope.row.orderID)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- dialogVisible to new order -->
    <el-dialog title="新增订单" v-model="dialogFormVisible">
      <el-form :model="addOrderForm">
        <el-form-item label="课程名称" :label-width="formLabelWidth">
          <el-select
            class="full-line"
            v-model="addOrderForm.courseId"
            filterable
            clearable
            placeholder="请选择课程名称">
            <el-option
              v-for="item in courseChoose"
              :label="item.courseNameCN"
              :value="item.courseId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教材名称" :label-width="formLabelWidth">
          <el-select
            class="full-line"
            v-model="addOrderForm.textbookId"
            filterable
            clearable
            placeholder="请选择教材名称">
            <el-option
              v-for="item in textbookChoose"
              :label="item.textbookName"
              :value="item.textbookId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用专业" :label-width="formLabelWidth">
          <el-select
            class="full-line"
            v-model="addOrderForm.majorId"
            filterable
            clearable
            placeholder="请选择专业名称">
            <el-option
              v-for="item in majorChoose"
              :label="item.majorName"
              :value="item.majorID">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用班级" :label-width="formLabelWidth">
          <el-select
            class="full-line"
            v-model="addOrderForm.classId"
            filterable
            clearable
            placeholder="请选择班级名称">
            <el-option
              v-for="item in classChoose"
              :label="item.className"
              :value="item.classId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预定数量" :label-width="formLabelWidth">
          <el-input v-model="addOrderForm.orderNum"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input type="textarea" :rows="5" v-model="addOrderForm.remark1"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="UpdateDialogForm">修改</el-button>
      </div>
    </el-dialog>

    <div class="paging">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<style>
</style>
<script>
  export default{
    name: 'confirmOrder',
    data(){
      return {
        form: {
          majorId: '',
          grade: '',
          academicYear: '',
          term: '',
          isConfirm: '0'
        },
        addOrderForm: {
          textbookId: '',
          publishingHouse: '',
          textbookName: '',
          author: '',
          price: '',
          isbn: '',

          courseId: '',
          courseNameCN: '',

          majorId: '',
          majorName: '',

          classId: '',
          orderNum: '',
          remark1: ''
        },
        updateOrder: {
          order: []
        },
        historyOrderTable: [],
        majorChoose: [],
        classChoose: [],
        courseChoose: [],
        textbookChoose: [],
        currentPage: 1,
        pageSizes: [10, 20, 30, 40],
        pageSize: 10,
        total: 40,
        dialogFormVisible: false,
        formLabelWidth: '120px',
      }
    },
    mounted: function () {
      this.$nextTick(function () {
        this.init();
      })
    },
    methods: {
      init(){
        this.getMajorInfo();
        this.getClassInfo();
        this.getCourseInfo();
        this.getTextbookInfo();
        this.getOrder();
      },
      getOrder(){
        this.$http.post(this.$until.url + '/textBookOrder/getTextBookOrder2', this.form,
          {
            params: {
              page: this.currentPage,
              pageSize: this.pageSize
            },
            emulateJSON: true
          }).then(response => {
          this.total = response.body.countNum;
          this.historyOrderTable = response.body.orderList
        }, response => {
          console.log('文件获取失败');
        });
      },
      searchOrder(){
        this.getOrder();
      },
      getMajorInfo: function () {
        this.$http.get(this.$until.url + '/baseInfoCommon/initData').then(response => {
          this.majorChoose = response.body.majorAndId
        }, response => {
          console.log('文件获取失败');
        });
      },
      getClassInfo: function () {
        this.$http.get(this.$until.url + '/baseInfoCommon/getClassNameAndId').then(response => {
          this.classChoose = response.body
        }, response => {
          console.log('文件获取失败');
        });
      },
      getCourseInfo: function () {
        this.$http.get(this.$until.url + '/textBookOrder/getCourseNameAndId').then(response => {
          this.courseChoose = response.body
        }, response => {
          console.log('文件获取失败');
        });
      },
      getTextbookInfo: function () {
        this.$http.get(this.$until.url + '/textBookManage/getTextBook').then(response => {
          this.textbookChoose = response.body.textBookList
        }, response => {
          console.log('文件获取失败');
        });
      },
      getGrade(val){
        this.form.grade = val
      },
      getAcademicYear(val){
        this.form.academicYear = val
      },
      handleSizeChange(val) {
        //console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.getOrder()
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getOrder()
      },
      updateOeder(index, rows){
        this.updateOrder.order = [];
        var msg = {}
        for (var key in rows[index]) {
          msg[key] = rows[index][key]
        }
        this.addOrderForm = msg
        this.dialogFormVisible = true
      },
      UpdateDialogForm(){
        this.updateOrder.order.push(this.addOrderForm)
        this.$http.post(this.$until.url + '/textBookOrder/updateOrder', this.updateOrder).then(res => {
          this.$message({
            type: 'success',
            message: res.body.message
          });
          this.getOrder()
        }, err => {
          this.$message.error('修改失败');
        });
        this.dialogFormVisible = false
      },
      deleteOrder(index, rows, orderId){
        this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteOrderOperation(orderId);
          rows.splice(index, 1);
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      deleteOrderOperation(orderId){
        this.$http.get(this.$until.url + '/textBookOrder/delOrder',
          {
            params: {
              orderId: orderId
            }
          }).then(res => {
          this.getOrder();
          this.$message({
            type: 'success',
            message: res.body.message
          });
          this.getOrder()
        }, err => {
          this.$message.error('删除失败');
        });
      },
      confirmOrder(){
        this.updateOrder.order = this.historyOrderTable;
        this.$http.post(this.$until.url + '/textBookOrder/updateIsConfirm', this.updateOrder).then(res => {
          this.$message({
            type: 'success',
            message: res.body.message
          });
          this.getOrder()
        }, err => {
          this.$message.error('提交失败');
        });
      }
    }
  }
</script>
